/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.autocode.global.adapter;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.stereotype.Component;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;

import cn.com.ksplatform.modules.autocode.dto.ColumnArray;
import cn.com.ksplatform.modules.autocode.manager.model.Sence;
import cn.com.ksplatform.modules.autocode.util.SenceUtil;
import cn.com.ksplatform.modules.autocode.util.ToolUtil;

/**
 * ClassName:Adapter_0.java <br/>
 * Date: 2015年11月17日 <br/>
 * 单表场景适配器
 * @author Mr.Xiao
 * @version 1.0
 */
@Component
public class Adapter_0 implements IAdapter{
	
	
	@Override
	public void doAdapter(VelocityConfigurer config,Sence senceModel, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ColumnArray columns = SenceUtil.getColumns(senceModel);
		
		HttpSession session = request.getSession();
		
		VelocityEngine ve = config.getVelocityEngine();
		Map param = new HashMap<String, Object>();
		param.putAll(ToolUtil.gettools());
		param.put("request", request);
		param.put("columns", columns);
		param.put("sence", senceModel);
		param.put("module", senceModel.getModule());
		
		param.putAll(SenceUtil.ClassifyColumns(columns));
		param.put("session",new HashMap<String,Object>());
		
		Enumeration<String> names = session.getAttributeNames();
		while(names.hasMoreElements()){
			String s = names.nextElement();
			Map sessionMap = (Map) param.get("session");
			sessionMap.put(s, session.getAttribute(s));
		}
		String html = VelocityEngineUtils.mergeTemplateIntoString(ve,"cn/com/ksplatform/modules/autocode/template/page/db.vm","UTF-8",param);
		response.getWriter().write(html);
	}
}
